#批量视频转换，从ts转换为mp4
import os
import re
from 查找没用的图片 import listDir,listDir2
import json
#字典
def movieDic():
    
    filepath = "aab.txt"
    dic = {}
    try:
        with open(filepath) as f:
    #         text = f.read()
            for x in json.load(f)['lectures']:
                for j in x["children"]:
                    arr = j["prefix"].split(".")
                    if len(arr[1]) == 1:
                        dic[j["_id"]] = [f"{arr[0]}.0{arr[1]}",j["name"]]
                    else:
                        dic[j["_id"]] = [f"{arr[0]}.{arr[1]}",j["name"]]
    #         print(text)
    except FileNotFoundError:
        print('无法打开指定的文件!')
    except LookupError:
        print('指定了未知的编码!')
    except UnicodeDecodeError:
        print(f'读取文件时解码错误!{filepath}')
    return dic

def takeFirst(elem):
    return float(elem[0])
def takeFirst3(elem):
    pattern1 = re.compile(r'(\d*)\.ts')
    results = re.findall(pattern1, elem)
    return int(results[0])

def takeFirst2(elem):
    arr = elem.split(".")
    if len(arr[1]) == 1:
        dic[j["_id"]] = [f"{arr[0]}.0{arr[1]}",j["name"]]
    else:
        dic[j["_id"]] = [f"{arr[0]}.{arr[1]}",j["name"]]
    return float(elem[0])
#改名
def changeName():
    _dir = "/Users/wangshuguan/Desktop/wmvideo/607d39165b8b1c00013395eb"
    for src in listDir2(_dir):
        dst = "_".join(dic[os.path.basename(src)])
        dst1 = f"/Users/wangshuguan/Desktop/wmvideo/607d39165b8b1c00013395eb/{dst}"
        os.rename(src, dst1)
#合并视频
def achiveMovie(dirname):
    _dir = f"/Users/wangshuguan/Desktop/wmvideo/607d39165b8b1c00013395eb/{dirname}"
    files = [os.path.basename(x) for x in os.listdir(_dir) if os.path.isfile(_dir+"/"+x)]
    files.sort(key=takeFirst3)
    sources = "|".join(files)
    return f'cd {_dir};\n~/Downloads/ffmpeg -i "concat:{sources}" -c copy /Users/wangshuguan/Desktop/wmvideo/{dirname}.mp4;\n'
def changeTargetName(path,dirname):
    pattern1 = re.compile(r'(.+)mobile_low(\d*)\.ts')
    for src in listDir(f"{path}{dirname}",["ts"]):
        results = re.findall(pattern1, os.path.basename(src))
        if len(results) > 0:
            if len(results[0])>1:
                dst = f"{path}{dirname}/{results[0][1]}.ts"
                if os.path.isfile(src):
                    os.rename(src, dst)
def renamePath():
    for x in listDir2("/Users/wangshuguan/Desktop/wmvideo/607d39165b8b1c00013395eb"):
        dirname = os.path.basename(x)
        changeTargetName("/Users/wangshuguan/Desktop/wmvideo/607d39165b8b1c00013395eb/",dirname)
def createSHFIle():
    
    str = "\n".join([achiveMovie(os.path.basename(x)) for x in listDir2(_dir)])
    with open("/Users/wangshuguan/Desktop/wmvideo/tt.sh", 'w+',encoding='utf-8') as f:
        f.write(str)
        
#字典
def movieDic2():
    
    filepath = "aab.txt"
    dic = {}
    try:
        with open(filepath) as f:
    #         text = f.read()
            i = 0
            for x in json.load(f)['lectures']:
                print(f'{i+1}-{x["name"]}')
                i = i+1
                # for j in x["children"]:
                #     arr = j["prefix"].split(".")
                #     if len(arr[1]) == 1:
                #         dic[j["_id"]] = [f"{arr[0]}.0{arr[1]}",j["name"]]
                #     else:
                #         dic[j["_id"]] = [f"{arr[0]}.{arr[1]}",j["name"]]
    #         print(text)
    except FileNotFoundError:
        print('无法打开指定的文件!')
    except LookupError:
        print('指定了未知的编码!')
    except UnicodeDecodeError:
        print(f'读取文件时解码错误!{filepath}')
    return dic
if __name__ == "__main__":
    movieDic2()
    # _dir = "/Users/wangshuguan/Desktop/wmvideo/607d39165b8b1c00013395eb"
#     print(achiveMovie("17.02_让机器学会思维—图灵机"))
    
        
#     [print(os.path.basename(x)) for x in listDir(_dir,["ts"])]